package tuilayer;
import java.util.*;
import controlLayer.*;
import modelLayer.*;

public class DVDUI {
    private static boolean valid;
    private DVDCtr dvdCtr;

    public DVDUI() {
        dvdCtr = new DVDCtr();
    }
    
    public void handleDVD(){
        mainMenuDVD();
    }
    
    private void mainMenuDVD() {
        boolean stop = false;
        while (!stop) {
            int choice = writeMainMenuDVD();
            switch (choice) {
                case 0:
                    stop = true;
                    break;
                case 1:
                    createDVD();
                    break;
                case 2:
                    updateDVD();
                    break;
                case 3:
                    findDVD();
                    break;
                case 4:
                    findDVDCopy();
                    break;
                case 5:
                    deleteDVD();
                    break;
                case 6:
                    addCopy();
                    break;
                    default: System.out.println("Please enter a valid choice.");
                }
            }
    }
    
    private int writeMainMenuDVD() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\n*****DVD Menu*****");
        System.out.println("(0) Back");
        System.out.println("(1) Create DVD");
        System.out.println("(2) Update DVD");
        System.out.println("(3) Find DVD");
        System.out.println("(4) Find DVD Copy");
        System.out.println("(5) Delete DVD");
        System.out.println("(6) Add Copy");
        System.out.println("\nType your choice:");
        
        int choice = ValidateInput.readInt("Please enter a valid choice.");
        
        if(ValidateInput.validateInt(choice))        
            return choice;
        else {
            return 999;
        }
    }
    
    private void createDVD() {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nType the DVD's title:");
        String title = keyboard.nextLine();
        if(ValidateInput.isInteger(title))
            if(Integer.parseInt(title) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateDVDTitle(title))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the DVD's title:");
                title = keyboard.nextLine();
            }
        }

        System.out.println("\nType the DVD's barcode:");
        int barcode = ValidateInput.readInt("Type the DVD's barcode:");
        if(barcode == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateNewBarcode(barcode))
                valid = true;
            else {
                System.out.println("\nThis barcode already exists.");
                System.out.println("Type the DVD's barcode:");
                barcode = ValidateInput.readInt("Type the DVD's barcode:");
            }
        }

        System.out.println("\nType the DVD's artist:");
        String artist = keyboard.nextLine();
        if(ValidateInput.isInteger(artist))
            if(Integer.parseInt(artist) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateDVDArtist(artist))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the DVD's artist:");
                artist = keyboard.nextLine();
            }
        }

        System.out.println("\nType the publication date:");
        String publicationDate = keyboard.nextLine();
        if(ValidateInput.isInteger(publicationDate))
            if(Integer.parseInt(publicationDate) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateDVDPublicationDate(publicationDate))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the publication date:");
                publicationDate = keyboard.nextLine();
            }
        }

        dvdCtr.createDVD(title, barcode, artist, publicationDate);

        System.out.println("\nDVD successfully created!");
    }
    
    private void updateDVD() {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("\nType the DVD's barcode:");
        int barcode = ValidateInput.readInt("Type the DVD's barcode:");
        if(barcode == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateBarcode(barcode))
                valid = true;
            else {
                System.out.println("\nThis barcode does not exist.");
                System.out.println("Type the DVD's barcode:");
                barcode = ValidateInput.readInt("Type the DVD's barcode:");
            }
        }

        System.out.println("\nType the DVD's new title:");
        String title = keyboard.nextLine();
        if(ValidateInput.isInteger(title))
            if(Integer.parseInt(title) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateDVDTitle(title))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the DVD's new title:");
                title = keyboard.nextLine();
            }
        }

        System.out.println("\nType the DVD's new artist:");
        String artist = keyboard.nextLine();
        if(ValidateInput.isInteger(artist))
            if(Integer.parseInt(artist) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateDVDArtist(artist))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the DVD's new artist:");
                artist = keyboard.nextLine();
            }
        }

        System.out.println("\nType the new publication date:");
        String publicationDate = keyboard.nextLine();
        if(ValidateInput.isInteger(publicationDate))
            if(Integer.parseInt(publicationDate) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateDVDPublicationDate(publicationDate))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the new publication date:");
                publicationDate = keyboard.nextLine();
            }
        }

        dvdCtr.updateDVD(barcode, title, artist, publicationDate);

        System.out.println("\nDVD successfully updated!");
    }
    
    private void findDVD() {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nType the DVD's barcode:");
        int barcode = ValidateInput.readInt("Type the DVD's barcode:");
        if(barcode == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateBarcode(barcode))
                valid = true;
            else {
                System.out.println("\nThis barcode does not exist.");
                System.out.println("Type the DVD's barcode:");
                barcode = ValidateInput.readInt("Type the DVD's barcode:");
            }
        }
        
        DVD dvd = dvdCtr.findDVD(barcode);
        System.out.println();
        System.out.println("Title:" + dvd.getTitle());
        System.out.println("Artist:" + dvd.getArtist());
        System.out.println("Publication Date:" + dvd.getPublicationDate());
        System.out.println("Number of copies:" + dvd.getNumberOfCopies());
    }
    
    private void findDVDCopy() {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nType the serial number:");
        int serialNumber = ValidateInput.readInt("Type the serial number:");
        if(serialNumber == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateSerialNumber(serialNumber))
                valid = true;
            else {
                System.out.println("\nThis serial number does not exist.");
                System.out.println("Type the serial number:");
                serialNumber = ValidateInput.readInt("Type the serial number:");
            }
        }
        
        Copy copy = dvdCtr.findDVDCopy(serialNumber);
        System.out.println();
        System.out.println("Purchase Date:" + copy.getPurchaseDate());
        System.out.println("Purchase Price" + copy.getPurchasePrice());
        System.out.println("Status:" + copy.getStatus());
    }
    
    private void deleteDVD() {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nType the DVD's barcode:");
        int barcode = ValidateInput.readInt("Type the DVD's barcode:");
        if(barcode == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateBarcode(barcode))
                valid = true;
            else {
                System.out.println("\nThis barcode does not exist.");
                System.out.println("Type the DVD's barcode:");
                barcode = ValidateInput.readInt("Type the DVD's barcode:");
            }
        }
        
        dvdCtr.deleteDVD(barcode);

        System.out.println("\nDVD successfully deleted!");
    }
    
    private void addCopy() {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("\nType the DVD's barcode:");
        int barcode = ValidateInput.readInt("Type the DVD's barcode:");
        if(barcode == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateBarcode(barcode))
                valid = true;
            else {
                System.out.println("\nThis barcode does not exist.");
                System.out.println("Type the DVD's barcode:");
                barcode = ValidateInput.readInt("Type the DVD's barcode:");
            }
        }
        
        System.out.println("\nType the serial number:");
        int serialNumber = ValidateInput.readInt("Type the serial number:");
        if(serialNumber == 0) return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validateNewSerialNumber(serialNumber))
                valid = true;
            else {
                System.out.println("\nThis serial number already exists.");
                System.out.println("Type the serial number:");
                serialNumber = ValidateInput.readInt("Type the serial number:");
            }
        }
        
        System.out.println("\nType purchase date:");
        String purchaseDate = keyboard.nextLine();
        if(ValidateInput.isInteger(purchaseDate))
            if(Integer.parseInt(purchaseDate) == 0)
                return;
        valid = false;
        while(!valid) {
            if (ValidateInput.validatePurchaseDate(purchaseDate))
                valid = true;
            else {
                System.out.println("\nPlease provide valid input.");
                System.out.println("Type the purchase date:");
                purchaseDate = keyboard.nextLine();
            }
        }
        
        System.out.println("\nType the purchase price:");
        int purchasePrice = ValidateInput.readInt("Type the purchase price:");
        if(purchasePrice == 0) return;
        
        dvdCtr.addCopy(barcode, serialNumber, purchasePrice, purchaseDate);
        
        System.out.println("\nDVD Copy successfully added!");
    }
}
